﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.WpfShell.ServiceRef;

namespace SmartSpace.WpfShell
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoginWindow window = new LoginWindow();
            if (window.ShowDialog() == true)
            {
                LoadNavigations();
            }
            else
            {
                Close();
            }
        }

        private void LoadNavigations()
        {
            ServiceRef.SsWebService client = App.GetProxy();
            ListResultOfNavigationModel result = client.GetNavigations();
            if (result.IsSuccess)
            {
                _ctlTreeNavigation.ItemsSource = result.Data;
            }
        }

        private void _ctlTreeNavigation_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            NavigationModel selectedNavigation = e.NewValue as NavigationModel;
            if (selectedNavigation != null)
            {
                object viewObj = this.GetType().Assembly.CreateInstance(selectedNavigation.ViewName);
                this._ctlFrame.Navigate(viewObj);
                if (viewObj is ISsView)
                {
                    (viewObj as ISsView).LoadArguments(SmartSpace.WpfShell.Utils.ArgumentsUtil.Prase(selectedNavigation.Arguments));
                }
            }
        }

        private void _ctlBtnHome_Click(object sender, RoutedEventArgs e)
        {
            object viewObj = this.GetType().Assembly.CreateInstance("SmartSpace.WpfShell.Modules.TaskListView");
            this._ctlFrame.Navigate(viewObj);
        }
    }
}
